package com.min.star.service.impl;

import java.util.Arrays;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.min.star.mapper.RoomMapper;
import com.min.star.model.Room;
import com.min.star.model.Grade;
import com.min.star.service.IRoomService;

@Service
public class RoomServiceImpl implements IRoomService {

	@Autowired
	private RoomMapper roomMapper;

	@Override
	public List<Room> selectRoomList() {
		return this.roomMapper.selectList(null);
	}

	@Override
	public Page<Room> selectPage(Page<Room> page, Room room) {
		QueryWrapper<Room> queryWrapper = new QueryWrapper<>();
		queryWrapper.lambda()
				// 如果出版社名称不为空，则对出版社进行模糊查询，是column列的值为 val

				.eq(!StringUtils.isEmpty(room.getStatus()), Room::getStatus, room.getStatus())

				.like(!StringUtils.isEmpty(room.getLocation()), Room::getLocation, room.getLocation());
		Page<Room> p = roomMapper.selectPage(page, queryWrapper);
		return p;
	}

	@Override
	public int add(Room room) {
		return this.roomMapper.insert(room);

	}

	@Override
	public int delete(String ids) {

		return this.roomMapper.deleteBatchIds(Arrays.asList(ids.split(",")));
	}

	@Override
	public boolean checkRoomTimeExist4Add(Room room) {
		QueryWrapper<Room> queryWrapper = new QueryWrapper<>();
		// 参数1：Model的get方法，形式是Model::get方法名 where publisher_name = '清华大学出版社'

		queryWrapper.lambda().eq(Room::getLocation, room.getLocation());
		// queryWrapper作为查询条件传递给selectCount方法
		Integer count = this.roomMapper.selectCount(queryWrapper);
		return count == 0 ? false : true;
	}

	@Override
	public Room save(Room room) {
		this.roomMapper.insert(room);
		Long id = room.getId();
		System.out.println("id in service =============" + id);
		return this.roomMapper.selectById(id);
	}

	@Override
	public boolean checkRoomTimeExist4Edit(Room room) {
		QueryWrapper<Room> queryWrapper = new QueryWrapper<>();
		// 参数1：Model的get方法，形式是Model::get方法名 where publisher_name = '清华大学出版社'

		queryWrapper.lambda().eq(Room::getLocation, room.getLocation());
		queryWrapper.lambda().ne(Room::getId, room.getId());
		// queryWrapper作为查询条件传递给selectCount方法
		Integer count = this.roomMapper.selectCount(queryWrapper);
		return count == 0 ? false : true;
	}

	@Override
	public Room update(Room room) {
		int ret = this.roomMapper.updateById(room);
		return this.roomMapper.selectById(room.getId());
	}

}
